package dbhandler;

import evthandler.EventMessage;
import filehandler.Reader;
import java.io.File;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.ArrayList;

/**
 * Class to handle the database connection
 * @author Casper, Dennis, Jackie, Jakob
 * @version 2.00
 */
public class DbCon {

    private Connection conn;
    private Statement stmt;
    private ResultSet result;
    public static final DbCon INSTANCE = new DbCon();

    /**
     * Private constructor prevents instantiation from other classes
     */
    private DbCon() {
        File file = new File("db.ini");
        if (!file.exists()) {
            new EventMessage("No db.ini file", "The db.ini file could not be found.\nPlease contact the system admin.").showMessage();
        } else {

            try {
                ArrayList<String> content = new Reader(file).readFile();
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = DriverManager.getConnection(content.get(0), content.get(1), content.get(2));
                stmt = conn.createStatement();
            } catch (FileNotFoundException ex) {
                new EventMessage("File not found", "The file could not be found at the expected place").showException(ex);
            } catch (ClassNotFoundException ex) {
                new EventMessage("JDBC Driver Class not found in DbCon constructor", "Could not find class\n").showException(ex);
            } catch (InstantiationException ex) {
                new EventMessage("Instantiation error in DbCon constructor", "Could not instantiate\n").showException(ex);
            } catch (IllegalAccessException ex) {
                new EventMessage("Illegal access in DbCon constructor", "Illegal access\n").showException(ex);
            } catch (SQLException ex) {
                new EventMessage("SQL error in DbCon constructor", "There was an error performing the sql inquery\n").showException(ex);
            }
        }
    }

    /**
     *
     * @return Returns a Singleton instance of DbCon
     */
    public static DbCon getInstance() {
        return INSTANCE;
    }

    /**
     * Used for SELECT
     * @param sqlQuery SQL Query String
     * @return Returns a ResultSet from the sqlQuery
     * @throws SQLException
     */
    public ResultSet executeQuery(String sqlQuery) throws SQLException {
        return stmt.executeQuery(sqlQuery);
    }

    /**
     * 
     * @param sqlQuery SQL Query String
     * @return Returns the primarykey as an int
     * @throws SQLException
     */
    public int insertAutoKey(String sqlQuery) throws SQLException {
        int autoKey = -1;
        stmt.executeUpdate(sqlQuery);
        result = stmt.executeQuery("select last_insert_id()");
        while (result.next()) {
            autoKey = result.getInt(1);
        }
        return autoKey;
    }

    /**
     * Used for INSERT, UPDATE & DELETE
     * @param sqlQuery SQL Query String
     * @throws SQLException
     */
    public void executeUpdate(String sqlQuery) throws SQLException {
        stmt.executeUpdate(sqlQuery);
    }
}
